package com.health.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.EventLog;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.health.R;
import com.health.db.FDbManager;
import com.health.fragment.jtys.JTYSDA;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tablebean.BD_DD_INFO;
import com.health.tools.GlobalConts;
import com.health.tools.PopData;
import com.health.view.spinner.NiceSpinner;
import com.health.viewscontrols.NovaApplication;
import com.health.viewscontrols.qyinfo.QYInfo;
import com.health.widget.LabelsView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static android.view.View.GONE;

/**
 * 家庭关系档案listAdapter
 * 
 * @author nova
 *
 */
public class MemberRelationAdapter extends BaseAdapter {

	private List<ARCHIVE_BASEINFO> infoList;
	/**
	 * 设置请选择的id 为000
	 */
	private static final String BD_DD_SELECT_ID="000";
	private Context context;
	/**
	 * 家庭关系
	 */
	List<BD_DD_INFO>   member_list = new ArrayList<BD_DD_INFO>();
	/**
	 * 设置下拉spinner  是否可以下拉选择
	 */
	private boolean isSpinnerFlag=true;

	public MemberRelationAdapter(Context context, List<ARCHIVE_BASEINFO> infoList ) {
		super();
		this.context = context;
		this.infoList = infoList;
	}


	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return infoList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return infoList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView==null) {
			holder = new ViewHolder();
			convertView = View.inflate(context, R.layout.member_relation_item, null);

			holder.jtys_list_name = (TextView) convertView.findViewById(R.id.jtys_list_name);
			holder.jtys_list_age = (TextView) convertView.findViewById(R.id.jtys_list_age);
			holder.jtys_list_sex = (TextView) convertView.findViewById(R.id.jtys_list_sex);
			holder.jtys_list_phone = (TextView) convertView.findViewById(R.id.jtys_list_phone);
			holder.jtys_list_idcard = (TextView) convertView.findViewById(R.id.jtys_list_idcard);
			holder.spinner_relationname=(NiceSpinner)convertView.findViewById(R.id.spinner_relationname);
			holder.btn_delete=(Button)convertView.findViewById(R.id.btn_delete);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		setData(holder, position, convertView);

		return convertView;
	}

	private class ViewHolder {

		TextView jtys_list_name;// 姓名
		TextView jtys_list_age;// 年龄
		TextView jtys_list_sex;
		TextView jtys_list_phone;// 手机号
		TextView jtys_list_idcard;// 地址
		NiceSpinner spinner_relationname;
		Button btn_delete;
	}

	/**
	 * 设置数据
	 * 
	 * @author 王婷婷
	 * @date 2017年4月8日 上午9:30:07
	 */
	private void setData(final ViewHolder holder, final int dataPos, View convertView) {

		final ARCHIVE_BASEINFO memInfo = infoList.get(dataPos);
		Log.e("adapter---", memInfo.toString());

		holder.jtys_list_name.setText(memInfo.getCUSTOMERNAME());
		int age = GlobalConts.getAge(memInfo.getBIRTHDAY());
		if (age != -1) {
			holder.jtys_list_age.setText(String.valueOf(age) + "岁");
		} else {
			holder.jtys_list_age.setText("");
		}


		List<BD_DD_INFO> bdxingbie = FDbManager.getInstance(context).findAllByWhereInInfoDB(BD_DD_INFO.class,
				"PARENTID = 'DM01-08'");
		String sex = "";
		for (BD_DD_INFO bdi : bdxingbie) {
			if (memInfo.getSEX().equals(bdi.getID())) {
				sex = bdi.getDATAINFO();
			}
		}

		holder.jtys_list_sex.setText(sex);
		holder.jtys_list_phone.setText(memInfo.getPHONE());

		List<BD_DD_INFO> sList1 = FDbManager.getInstance(context).findAllByWhereInInfoDB(BD_DD_INFO.class,
				  " PARENTID='DM01-06' order by cast(SORT as int) asc");
		int size = sList1.size();
		member_list = new ArrayList<BD_DD_INFO>();
//		BD_DD_INFO bd_dd_relation_p = new BD_DD_INFO();
//		bd_dd_relation_p.setDATAINFO("请选择家庭关系");
//		bd_dd_relation_p.setDATANUM("111111");
//		bd_dd_relation_p.setID(BD_DD_SELECT_ID);
//		bd_dd_relation_p.setPARENTID("DM01-06");
//		bd_dd_relation_p.setSORT("0");
//		bd_dd_relation_p.setNBPHSC("Y");
//		bd_dd_relation_p.setPrintNum("0");
//		member_list.add(bd_dd_relation_p);
		if (size > 0) {

			for (int i = 0; i < size; i++) {
				String str_id = sList1.get(i).getID();
				Log.e("家庭关系",str_id+sList1.get(i).getDATAINFO());
				if (!"DM01-06_0".equals(str_id) ) {
					member_list.add(sList1.get(i));
				}
			}

			holder.spinner_relationname.attachDataSource(member_list);
			holder.spinner_relationname.setSelectedIndex(member_list.size()-1);
		}



		/**
		 * 设置是否可以点击
		 */
		if(isSpinnerFlag){
			holder.spinner_relationname.setEnabled(true);
			holder.spinner_relationname.setClickable(true);
			holder.spinner_relationname.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
				@Override
				public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
					memInfo.setRELATIONID(member_list.get(holder.spinner_relationname.getSelectedIndex()).getID());
					eventActionCallback.updatePosition(dataPos,memInfo);
				}

				@Override
				public void onNothingSelected(AdapterView<?> parent) {

				}
			});
			holder.btn_delete.setVisibility(View.VISIBLE);
			holder.btn_delete.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
//				position = Integer.parseInt(v.getTag().toString());// TODO
					eventActionCallback.deletePosition(dataPos);
				}
			});
		}else{
			holder.spinner_relationname.setEnabled(false);
			holder.spinner_relationname.setClickable(false);
			holder.spinner_relationname.setSelectedIndex(getListIndex(memInfo.getRELATIONID(), member_list));


			holder.btn_delete.setVisibility(View.GONE);
		}



		//            nice_spinner_fyycx.setSelectedIndex(getListIndex(readFollowup.getMEDICATIONCOMPLIANCE(), ywycx_list));
//		ywycx_list.get(nice_spinner_fyycx.getSelectedIndex()).getID()

	}

	/**
	 * 设置更新数据
	 * @param infoList
	 */
	public void setDataList(List<ARCHIVE_BASEINFO> infoList,boolean spinnerFlag){
		this.infoList = infoList;
		isSpinnerFlag=spinnerFlag;
	}

	/**
	 * 设置 数据展示
	 * @param str
	 * @param bds
	 * @return
	 */
	private int getListIndex(String str, List<BD_DD_INFO> bds) {
		if (str != null && !str.equals("")) {

			for (int i = 0; i < bds.size(); i++) {
				if (str.equals(bds.get(i).getID())) {
					return i;
				}
			}
		}
		return 0;
	}
	/**
	 * 获取spinner的值
	 */
	private EventActionCallback eventActionCallback;

	public interface EventActionCallback {

		/**
		 * 删除数据方法
		 * @param deletePosition
		 */
		void deletePosition(int deletePosition);
		/**
		 * 修改数据方法
		 * @param updatePosition,relationType
		 */
		void updatePosition(int updatePosition,  ARCHIVE_BASEINFO memberInfo);

	}
	public void setEventActionCallback(EventActionCallback  Callback ){
		this.eventActionCallback=Callback;
	}



}
